package demo1;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class CardList {
    private static final String[] suits = {"♦","♥","♠","♣"};


    //生成一副牌
    public static List<Card> buyCard(){
        List<Card> list = new ArrayList<>();
        for (int i = 0; i < suits.length; i++) {
            for (int j = 0; j < 13; j++) {
                Card card = new Card(suits[i], j);
                list.add(card);
            }
        }
        return  list;
    }


    //洗牌
    public static void shuffle(List<Card> list){
        for (int i = 1; i <= list.size()-1; i++) {
            Random random = new Random();
            int index = random.nextInt(i);
            swap(list,i,index);
        }
    }

    //打乱
    private static void swap(List<Card> list,int i,int j) {
        Card tmp = list.get(i);
        list.set(i,list.get(j));
        list.set(j,tmp);

    }

    public static void main(String[] args) {
        List<Card> cards =   buyCard();
//        System.out.println(cards);

        shuffle(cards);
        System.out.println(cards);

        List<List<Card>> hand = new ArrayList<>();

        //三个人
        List<Card> hand1 = new ArrayList<>();
        List<Card> hand2 = new ArrayList<>();
        List<Card> hand3 = new ArrayList<>();


        hand.add(hand1);
        hand.add(hand2);
        hand.add(hand3);

        //摸牌
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                Card card= cards.remove(0);
                hand.get(j).add(card);
            }
        }

        System.out.println("第1个人的牌："+hand1);
        System.out.println("第2个人的牌："+hand2);
        System.out.println("第3个人的牌："+hand3);

        System.out.println("剩下的牌："+cards);
    }
}
